package Utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class mybatisUtils {
    private static SqlSessionFactory factory=null;
    static {
        String config="mybatis.xml";//需要和项目中resources名一样
        //2.读取这个config表示的文件
        try {
            InputStream input= Resources.getResourceAsStream(config);
            //创建SqlSessionFactory
            factory=new  SqlSessionFactoryBuilder().build(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取SqlSession方法
    public static SqlSession getSqlSession() {
        SqlSession session=null;
        if (null!=factory)
            session= factory.openSession();//非自动提交事务
        return session;
    }
}